<!DOCTYPE html>
<html lang="en">
<head>
    <title>识别记录</title>
    {% include 'admin/common/header.html' %}
    <link rel="stylesheet" href="{{ url_for('static', filename='/admin/admin/css/other/user.css') }}"/>
</head>
<body class="pear-container">
{# 查询表单 #}
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="" lay-filter="user-query-form">
            <div class="layui-form-item">
                <label class="layui-form-label">病害名称</label>
                <div class="layui-input-inline">
                    <input type="text" name="disease_name" placeholder="" class="layui-input">
                </div>

                <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="user-query">
                    <i class="layui-icon layui-icon-search"></i>
                    查询
                </button>
                <button type="reset" class="pear-btn pear-btn-md">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </form>
    </div>
</div>

{# 用户表格 #}
<div class="user-main user-collasped">
    <div class="layui-card">
        <div class="layui-card-body">
            <table id="history-table" lay-filter="history-table"></table>
        </div>
    </div>
</div>
</body>

{# 表格操作 #}
<script type="text/html" id="user-toolbar">

    {% if authorize("pest:remove") %}
        <button class="pear-btn pear-btn-md" lay-event="batchRemove">
            <i class="pear-icon pear-icon-ashbin"></i>
            删除
        </button>
    {% endif %}

</script>

{# 用户修改操作 #}
<script type="text/html" id="user-bar">

    {% if authorize("pest:remove") %}
        <button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
                class="pear-icon pear-icon-ashbin"></i>
        </button>
    {% endif %}
</script>



{# 用户注册时间 #}
<script type="text/html" id="user-createTime">
    {{ '  {{layui.util.toDateString(d.create_at,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>

{# 用户更新时间 #}
<script type="text/html" id="user-updateTime">
    {{ '  {{layui.util.toDateString(d.update_at,  "yyyy-MM-dd HH:mm:ss")}' |safe }}}
</script>

{% include 'admin/common/footer.html' %}

<script>
  layui.use(['table', 'dtree', 'form', 'jquery', 'popup', 'common'], function () {
    let table = layui.table
    let form = layui.form
    let $ = layui.jquery
    let dtree = layui.dtree
    let popup = layui.popup
    let common = layui.common
    let MODULE_PATH = '/'

    // 表格数据
    let cols = [
      [
        {% if authorize("pest:remove") %}
          { type: 'checkbox' },
        {% endif %}
        {
            title: '病害图片',
            field: 'search_img',
            unresize: true,
            templet: function (d) {
                return '<img class="photo" style="max-width: 100%;\n' +
                  '  height: 100%;\n' +
                  '  cursor: pointer;" lay-event="photo" src=" ' + d.search_img + '"></i>'
            },
            align: 'center',
            width: 100
        },
        { title: '病害名称', field: 'disease_name', align: 'center' },
        { title: '查询准确率', field: 'search_acuracy', align: 'center' },

        { title: '查询用户', field: 'user_id', align: 'center' },
        { title: '查询时间', field: 'search_time', align: 'center' },

        { title: '操作', toolbar: '#user-bar', align: 'center', width: 200 }
      ]
    ]

    // 渲染表格数据
    table.render({
      elem: '#history-table',
        {% if authorize("history:admin") %}
      url: '/pest_history/info',
        {% endif %}
        {% if authorize("history:user") %}
      url: '/pest_history/user_info',
        {% endif %}
      page: true,
      cols: cols,
      skin: 'line',
      height: 'full-148',
      toolbar: '#user-toolbar', /*工具栏*/
      text: { none: '暂无历史查询记录' },
      defaultToolbar: [{ layEvent: 'refresh', icon: 'layui-icon-refresh' }, 'filter', 'print', 'exports'] /*默认工具栏*/
    })


    table.on('tool(history-table)', function (obj) {
      if (obj.event === 'remove') {
        window.remove(obj)
      }  else if (obj.event === 'photo') {
        window.photo(obj)
      }
    })

    table.on('toolbar(history-table)', function (obj) {
      if (obj.event === 'refresh') {
        window.refresh()
      } else if (obj.event === 'batchRemove') {
        window.batchRemove(obj)
      }
    })

    form.on('submit(user-query)', function (data) {
      window.refresh(data.field)
      return false
    })


    // 单独删除
    window.remove = function (obj) {
      layer.confirm('确定要删除该记录', { icon: 3, title: '提示' }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({
          url: '/pest_history/remove/' + obj.data['history_id'],
          dataType: 'json',
          type: 'delete',
          success: function (result) {
            layer.close(loading)
            if (result.success) {
              popup.success(result.msg, function () {
                obj.del()
              })
            } else {
              popup.failure(result.msg)
            }
          }
        })
      })
    }

    // 批量删除
    window.batchRemove = function (obj) {
      let data = table.checkStatus(obj.config.id).data
        console.log("选中的数据是", data)
      if (data.length === 0) {
        layer.msg('未选中数据', {
          icon: 3,
          time: 1000
        })
        return false
      }
      var ids = []
      var hasCheck = table.checkStatus('history-table')
      var hasCheckData = hasCheck.data
      if (hasCheckData.length > 0) {
        $.each(hasCheckData, function (index, element) {
          ids.push(element.history_id)
        })
      }
      console.log(ids);
      layer.confirm('确定要删除选中识别记录', {
        icon: 3,
        title: '提示'
      }, function (index) {
        layer.close(index)
        let loading = layer.load()
        $.ajax({

          url: '/pest_history/batchRemove',
          data: { ids: ids },
          dataType: 'json',
          type: 'delete',
          success: function (result) {
            layer.close(loading)
            if (result.success) {
              popup.success(result.msg, function () {
                table.reload('history-table')
              })
            } else {
              popup.failure(result.msg)
            }
          }
        })
      })
    }

    window.refresh = function (param) {
      table.reload('history-table', { where: param })
    }

      // 查看大图
    window.photo = function (obj) {
      if (!obj.data.search_img || obj.data.search_img === '') {
        layer.msg('图片地址错误！')
        return
      }
      var auto_img = {}
      var img = new Image()
      img.src = obj.data.search_img
      img.onload = function () {
        var max_height = $(window).height() - 100
        var max_width = $(window).width()
        var rate1 = max_height / img.height
        var rate2 = max_width / img.width
        var rate3 = 1
        var rate = Math.min(rate1, rate2, rate3)
        auto_img.height = img.height * rate
        auto_img.width = img.width * rate
        layer.open({
          type: 1,
          title: false,
          area: ['auto'],
          skin: 'layui-layer-nobg', //没有背景色
          shadeClose: true,
          content: '<img src=\'' + obj.data['search_img'] + '\' width=\'' + auto_img.width + 'px\' height=\'' + auto_img.height + 'px\'>'
        })
      }
    }

  })
</script>

</html>